package com.server.llh.llhserverwork.view.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.bean.MyOrgInfoData;

import java.util.List;

/**
 * Created by ZTF on 2017-06-29.
 */

public class MyPopWindow extends PopupWindow{
    private View conentView;
    ListView llh_org_info;
    List<MyOrgInfoData.DataBean> myOrgInfoDataList=null;
    ItemClickCallBack itemClickCallBack=null;
    Context context;
    OrgInfoAdapter orgInfoAdapter;
    TextView search_all;
    View view;
    public MyPopWindow(final Activity context, final List<MyOrgInfoData.DataBean> myOrgInfoDataList, final ItemClickCallBack itemClickCallBack){
       super();
        this.context=context;
        this.myOrgInfoDataList=myOrgInfoDataList;
        this.itemClickCallBack=itemClickCallBack;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.llh_my_popup_window, null);

        view = LayoutInflater.from(context).inflate(R.layout.llh_search_canting, null);
        search_all = (TextView) view.findViewById(R.id.search_all);
        llh_org_info= (ListView) conentView.findViewById(R.id.llh_org_info);
        llh_org_info.addFooterView(view);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w / 2 + 40);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview);
        this.orgInfoAdapter=new OrgInfoAdapter();
        this.llh_org_info.setAdapter(orgInfoAdapter);
        this.orgInfoAdapter.notifyDataSetChanged();
        this.llh_org_info.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        llh_org_info.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               if (ListView.CHOICE_MODE_SINGLE==llh_org_info.getChoiceMode()){
                   if (itemClickCallBack!=null){

                       MyOrgInfoData.DataBean dataBean=myOrgInfoDataList.get(position);
                       itemClickCallBack.CallBack(dataBean);
                       MyPopWindow.this.dismiss();
                   }
               }
            }
        });
        search_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyOrgInfoData.DataBean dataBean=new MyOrgInfoData.DataBean();
                dataBean.setOrgName("搜索周边");
                dataBean.setOrgId("0");
                itemClickCallBack.CallBack(dataBean);
                MyPopWindow.this.dismiss();
            }
        });

    }
    private class OrgInfoAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            if (myOrgInfoDataList!=null){
                return myOrgInfoDataList.size();
            }
            return 0;
        }
        @Override
        public Object getItem(int position) {
            if (myOrgInfoDataList!=null){
                return myOrgInfoDataList.get(position);
            }
            return 0;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView==null){
                viewHolder=new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.my_accout_info_item, null);
                viewHolder.cardNoBalance = (TextView) convertView.findViewById(R.id.card_banlance);
                viewHolder.cardNoTxt = (TextView) convertView.findViewById(R.id.cardName);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            MyOrgInfoData.DataBean myOrgInfoData=  myOrgInfoDataList.get(position);
            viewHolder.cardNoTxt.setText(""+myOrgInfoData.getOrgName());
            return convertView;
        }

        class ViewHolder{
            TextView cardNoBalance,cardNoTxt;
        }
    }



    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 5);
        } else {
            this.dismiss();
        }
    }

    public interface ItemClickCallBack{
        void CallBack(MyOrgInfoData.DataBean myOrgInfoData);
    }

}
